package dao.mysql;
import java.util.ArrayList;
import java.sql.*;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import BizLogic.BadDataException;

import model.LoginCredentials;
import dao.LoginCredentialsDAO;
public class LoginCredentialsDAOImpl implements LoginCredentialsDAO {
 
	DataSource dataSource=null;
	Connection con=null;
	PreparedStatement pstmt=null;
public LoginCredentialsDAOImpl(){
	try{
	//dataSource=(DataSource) new InitialContext().lookup("java:comp/env/jdbc/fbs");
	//con=dataSource.getConnection();
		con=ConnectionProperties.getConnection();
	}
	catch(Exception ee){
	System.out.println(ee.getMessage());
	}
}
public ArrayList<String> findAllManagers() throws SQLException,BadDataException{
	// TODO Auto-generated method stub
	ArrayList<String> managerArrayList=new ArrayList<String>();
	try {
		pstmt=con.prepareStatement("select name from staff s,logincredentials l  where s.StaffID=l.StaffID and l.role='admin'");
		ResultSet rs=pstmt.executeQuery();
		while(rs.next()){
			String manager=null;
				manager=rs.getString("name");
			managerArrayList.add(manager);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	finally
	{
		pstmt.close();
		con.close();
	}
	
	return managerArrayList;
}
	@Override
	public void insertLoginCredentials(LoginCredentials loginCredentials) throws SQLException{
		// TODO Auto-generated method stub
		try {
			pstmt=con.prepareStatement("insert into LoginCredentials values (?,?,?,?,?,?)");
			pstmt.setString(1, loginCredentials.getStaffID());
			pstmt.setString(2, loginCredentials.getPassword());
			pstmt.setString(3, loginCredentials.getRole());
			pstmt.setString(4, loginCredentials.getSalt());
            pstmt.setString(5, loginCredentials.getSecurityQuestion());
            pstmt.setString(6, loginCredentials.getSecurityAnswer());
			pstmt.executeUpdate();
		} catch (SQLException e) {
		
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
		
	}

	@Override
	public void updateLoginCredentials(LoginCredentials loginCredentials) throws SQLException{
		// TODO Auto-generated method stub
		try {
			pstmt=con.prepareStatement("update LoginCredentials set Password=?,Role=?,Salt=?,SecurityQuestion=?,SecurityAnswer=? where StaffID=?");
			pstmt.setString(1,loginCredentials.getPassword() );
			pstmt.setString(2, loginCredentials.getRole());
			pstmt.setString(3, loginCredentials.getSalt());
			pstmt.setString(4, loginCredentials.getSecurityQuestion());
			pstmt.setString(5, loginCredentials.getSecurityAnswer());
			pstmt.setString(6, loginCredentials.getStaffID());
			pstmt.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
	}
	@Override
	public void updatePassword(String StaffID, String Password, String Salt)
	{
		try{
		pstmt=con.prepareStatement("update LoginCredentials set Password=?,Salt=? where StaffID=?");
		pstmt.setString(1, Password);
		pstmt.setString(2, Salt);
		pstmt.setString(3, StaffID);
		pstmt.executeUpdate();
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			try {
				pstmt.close();
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
		// TODO Auto-generated method stub
		

	@Override
	public void updateByID(String staffID,String role)throws SQLException{
		try{
		pstmt=con.prepareStatement("update LoginCredentials set Role=?  where StaffID=?");
		pstmt.setString(1, role);
		pstmt.setString(2,staffID);
		pstmt.executeUpdate();
		}catch (SQLException e){
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
	}
	@Override
	public void deleteLoginCredentials(String StaffID) throws SQLException{
		// TODO Auto-generated method stub
		try {
			pstmt=con.prepareStatement("delete from LoginCredentials where StaffID=?");
			pstmt.setString(1, StaffID);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
		
		
	}

	@Override
	public LoginCredentials findLoginCredentials(String StaffID) throws SQLException{
		// TODO Auto-generated method stub
		LoginCredentials lc=null;
		try {
			pstmt=con.prepareStatement("select * from LoginCredentials where StaffID=?");
			pstmt.setString(1, StaffID);
			ResultSet rs=pstmt.executeQuery();
			
			if(rs.next()){
				lc=new LoginCredentials();
				lc.setStaffID(rs.getString("StaffID"));
				lc.setPassword(rs.getString("Password"));
				lc.setRole(rs.getString("Role"));
				lc.setSalt(rs.getString("Salt"));
				lc.setSecurityQuestion(rs.getString("SecurityQuestion"));
				lc.setSecurityAnswer(rs.getString("SecurityAnswer"));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
		return lc;
	
	}

	@Override
	public ArrayList<LoginCredentials> findAllLoginCredentials() throws SQLException{
		// TODO Auto-generated method stub
		ArrayList<LoginCredentials> lcArrayList=new ArrayList<LoginCredentials>();
		try {
			pstmt=con.prepareStatement("select * from LoginCredentials");
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()){
				LoginCredentials lc=new LoginCredentials();
				lc.setStaffID(rs.getString("StaffID"));
				lc.setPassword(rs.getString("Password"));
				lc.setRole(rs.getString("Role"));
				lc.setSalt(rs.getString("Salt"));
				lc.setSecurityQuestion(rs.getString("SecurityQuestion"));
				lc.setSecurityAnswer(rs.getString("SecurityAnswer"));
				lcArrayList.add(lc);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			pstmt.close();
			con.close();
		}
		
		return lcArrayList;
	}

}
